package org.yxj.videolibrary.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import org.yxj.videolibrary.R;
import org.yxj.videolibrary.i.PlayVideoListener;
import org.yxj.videolibrary.util.SubmergedBarUtil;


/**
 * 播放器的頭view
 *
 * @author on 2022/9/9
 */
public class PlayHeadView extends FrameLayout {


    private View view ;

    private LinearLayout headCommonView ;

    private ImageView back ;

    /**
     *  用户交互的回调事件
     */
    private PlayVideoListener playVideoListener ;

    public void setPlayVideoListener(PlayVideoListener playVideoListener) {
        this.playVideoListener = playVideoListener;
    }

    public PlayHeadView( Context context) {
        this(context,null);
    }


    public PlayHeadView( Context context,  AttributeSet attrs) {
        this(context, attrs,0);
    }


    public PlayHeadView( Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackground(getResources().getDrawable(R.drawable.video_head_bg));
        addView(view = LayoutInflater.from(context).inflate(R.layout.video_head_layout,this,false));
        headCommonView = view.findViewById(R.id.headCommonView);
        back = view.findViewById(R.id.back);
        view.findViewById(R.id.back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (playVideoListener!=null){
                    playVideoListener.onHeadBack();
                }
            }
        });
    }


    /**
     * @param drawable
     */
    public void setHeadCancelIcon(Drawable drawable,MarginLayoutParams margin){
       if (drawable!=null){
           back.setBackground(drawable);
       }

       if (margin!=null){
           MarginLayoutParams m = (MarginLayoutParams) back.getLayoutParams();
           m.leftMargin  = margin.leftMargin;
           back.setLayoutParams(m);
       }
    }




    /**
     * 是否支持沉嵌式状态栏
     * @param supportSubmergedBar
     */
    public void setSupportSubmergedBar(boolean supportSubmergedBar) {
        if (supportSubmergedBar && Build.VERSION.SDK_INT >= 19){
            setBackgroundResource(R.drawable.video_head_bg);
            setPadding(0, SubmergedBarUtil.getStatusBarHeight(getContext()),0,0);
        }else {
            setPadding(0, 0,0,0);
            setBackgroundResource(0);
        }
    }



    /**
     *
     * @param view
     */
    public void addHeadView(View view){
        if (view!=null){
            headCommonView.addView(view);
        }
    }

}
